/////////////////////////////////////////////////////////////////////////////
// Name:        dgt_StoreTab.cpp
// Purpose:     
// Author:      Aleksey Plotnikov & Alla Nabatova
// Modified by: 
// Created:     19/03/2008 23:56:23
// RCS-ID:      
// Copyright:   Diamant Software
// Licence:     
/////////////////////////////////////////////////////////////////////////////

#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
#pragma implementation "dgt_StoreTab.h"
#endif

// For compilers that support precompilation, includes "wx/wx.h".
#include "wx/wxprec.h"

#ifdef __BORLANDC__
#pragma hdrstop
#endif

#ifndef WX_PRECOMP
#include "wx/wx.h"
#endif

////@begin includes
////@end includes

#include "dgt_StoreTab.h"
#include "dgt_PluginManager.h"
#include "dgt_StudioApp.h"
#include "dgt_StorePlugin.h"

////@begin XPM images
////@end XPM images


/*!
 * dgt_StoreTab type definition
 */

IMPLEMENT_DYNAMIC_CLASS( dgt_StoreTab, wxPanel )


/*!
 * dgt_StoreTab event table definition
 */

BEGIN_EVENT_TABLE( dgt_StoreTab, wxPanel )

////@begin dgt_StoreTab event table entries
    EVT_GRID_CELL_LEFT_CLICK( dgt_StoreTab::OnCellLeftClick )

////@end dgt_StoreTab event table entries

END_EVENT_TABLE()


/*!
 * dgt_StoreTab constructors
 */

dgt_StoreTab::dgt_StoreTab()
{
    Init();
}

dgt_StoreTab::dgt_StoreTab(wxWindow* parent, wxWindowID id, const wxPoint& pos, const wxSize& size, long style)
{
    Init();
    Create(parent, id, pos, size, style);
}


/*!
 * dgt_StoreTab creator
 */

bool dgt_StoreTab::Create(wxWindow* parent, wxWindowID id, const wxPoint& pos, const wxSize& size, long style)
{
////@begin dgt_StoreTab creation
    wxPanel::Create(parent, id, pos, size, style);
    CreateControls();
////@end dgt_StoreTab creation
    return true;
}


/*!
 * dgt_StoreTab destructor
 */

dgt_StoreTab::~dgt_StoreTab()
{
////@begin dgt_StoreTab destruction
////@end dgt_StoreTab destruction
}


/*!
 * Member initialisation
 */

void dgt_StoreTab::Init()
{
////@begin dgt_StoreTab member initialisation
    m_StoreGrid = NULL;
    m_AboutPl = NULL;
////@end dgt_StoreTab member initialisation
}


/*!
 * Control creation for dgt_StoreTab
 */

void dgt_StoreTab::CreateControls()
{    
////@begin dgt_StoreTab content construction
    dgt_StoreTab* itemPanel1 = this;

    this->SetHelpText(_("Store plugins"));
    if (ShowToolTips())
        this->SetToolTip(_("Store plugins"));
    this->SetName(_T("Store"));
    wxBoxSizer* itemBoxSizer2 = new wxBoxSizer(wxVERTICAL);
    itemPanel1->SetSizer(itemBoxSizer2);

    wxBoxSizer* itemBoxSizer3 = new wxBoxSizer(wxHORIZONTAL);
    itemBoxSizer2->Add(itemBoxSizer3, 1, wxGROW|wxALL, 5);

    m_StoreGrid = new dgt_PluginGrid( itemPanel1, ID_GRID2, wxDefaultPosition, wxDefaultSize, wxSUNKEN_BORDER|wxHSCROLL|wxVSCROLL );
    m_StoreGrid->SetDefaultColSize(50);
    m_StoreGrid->SetDefaultRowSize(25);
    m_StoreGrid->SetColLabelSize(25);
    m_StoreGrid->SetRowLabelSize(50);
    m_StoreGrid->CreateGrid(1, 4, wxGrid::wxGridSelectCells);
    itemBoxSizer3->Add(m_StoreGrid, 1, wxGROW|wxALL, 5);

    m_AboutPl = new wxStaticText( itemPanel1, wxID_STATIC, _("Select a plugin"), wxDefaultPosition, wxDefaultSize, wxALIGN_LEFT );
    m_AboutPl->SetFont(wxFont(10, wxSWISS, wxNORMAL, wxNORMAL, false, wxT("Tahoma")));
    itemBoxSizer2->Add(m_AboutPl, 0, wxALIGN_LEFT|wxALL, 5);

////@end dgt_StoreTab content construction
	m_StoreGrid->SetGridMode();	
	m_StoreGrid->SetCheckBox(1);
	SetPluginInfo();
}


/*!
 * Should we show tooltips?
 */

bool dgt_StoreTab::ShowToolTips()
{
    return true;
}

/*!
 * Get bitmap resources
 */

wxBitmap dgt_StoreTab::GetBitmapResource( const wxString& name )
{
    // Bitmap retrieval
////@begin dgt_StoreTab bitmap retrieval
    wxUnusedVar(name);
    return wxNullBitmap;
////@end dgt_StoreTab bitmap retrieval
}

/*!
 * Get icon resources
 */

wxIcon dgt_StoreTab::GetIconResource( const wxString& name )
{
    // Icon retrieval
////@begin dgt_StoreTab icon retrieval
    wxUnusedVar(name);
    return wxNullIcon;
////@end dgt_StoreTab icon retrieval
}
/******************************************************************/
//Set info about plugin into the grid
void dgt_StoreTab::SetPluginInfo()
{
	dgt_StudioApp *tApp =(dgt_StudioApp*) wxApp::GetInstance();
	dgt_PluginManager *pManager = tApp->get_Manager();

	dgt_PluginInfo info;
	int row_numb = 0;
	unsigned qRows = 1;

	int number = pManager->get_PluginCount( PLUGIN_STORE );
	if( !number )
		return;

	int RowToAdd;
	if( qRows == 1)
		RowToAdd = number - 1;
	else
		RowToAdd = number;

	qRows += number;

	if( RowToAdd )
		m_StoreGrid->AppendRows( RowToAdd );

	for( int i = number; i > 0; i-- )
	{ 
		dgt_Plugin_Ptr plugin = pManager->get_Plugin( PLUGIN_STORE, i - 1 );

		if( pManager->IsLoaded( i - 1 ))
		{
			m_StoreGrid->SetCellValue( _T("1"), row_numb, 1 );
		}

		plugin->GetPluginInfo( info );
		wxString sDllName = pManager->get_PluginDllName( PLUGIN_STORE, i - 1 );
		m_StoreGrid->SetCellValue(sDllName,row_numb,0);

		wxString t_str = info.m_PluginName;
		m_StoreGrid->SetCellValue(t_str,row_numb,2);

		t_str = info.m_Version;
		m_StoreGrid->SetCellValue(t_str,row_numb,3);

		t_str = info.m_Description;
		array_string.Add(info.m_Description);

		m_StoreGrid->SetReadOnly(row_numb,0);
		m_StoreGrid->SetReadOnly(row_numb,2);
		m_StoreGrid->SetReadOnly(row_numb,3);
		row_numb++;
	}
}

/*!
 * wxEVT_GRID_CELL_LEFT_CLICK event handler for ID_GRID2
 */
//load info about selected plugin into the static text
void dgt_StoreTab::OnCellLeftClick( wxGridEvent& event )
{
	if (m_StoreGrid->GetCellValue(0,0)!=_T(""))
	{
		wxString string;
		int n=event.GetRow();
		string = array_string.Item(n);
		m_AboutPl->SetLabel(string);
	}
	
	event.Skip();

}

